package com.insurance.datasource.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.insurance.modules.app.utils.LoginSysUserContext;
import com.insurance.modules.claim.constants.ClaimConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * mybatis-plus 自动填充
 */
@Component
@Slf4j
public class EntityMetaObjectHandler implements MetaObjectHandler {
    private final static String ENABLE_FLAG = "enable_flag";
    private final static String CREATE_TIME = "createTime";
    private final static String UPDATE_TIME = "updateTime";
    private final static String CREATE_BY = "createBy";
    private final static String UPDATE_BY = "updateBy";

    @Override
    public void insertFill(MetaObject metaObject) {

        String[] names = metaObject.getGetterNames();
        //此处逻辑自己修改
        if (names != null && names.length > 0) {
            Date now = new Date();
            for (String name : names) {
                switch (name) {
                    case ENABLE_FLAG:
                        this.strictInsertFill(metaObject, ENABLE_FLAG, () -> ClaimConstants.FLAG_YES, Integer.class);
                        break;
                    case CREATE_TIME:
                        this.strictInsertFill(metaObject, CREATE_TIME, () -> now, Date.class);
                        break;
                    case UPDATE_TIME:
                        this.strictInsertFill(metaObject, UPDATE_TIME, () -> now, Date.class);
                        break;
                    case CREATE_BY:
                    case UPDATE_BY:
                        if (LoginSysUserContext.getLoginUserId() != null) {
                            Long userId = LoginSysUserContext.getLoginUserId();
                            this.strictInsertFill(metaObject, UPDATE_BY, () -> userId, Long.class);
                            this.strictInsertFill(metaObject, CREATE_BY, () -> userId, Long.class);
                            break;
                        }
                    default:
                        break;
                }
            }
        } else {
            log.warn("mybatis-plus 自动更新 获取属性失败");
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String[] names = metaObject.getGetterNames();
        if (LoginSysUserContext.getLoginUserId() != null) {
            if (names != null && names.length > 0) {
                Date now = new Date();
                for (String name : names) {
                    switch (name) {
                        case UPDATE_TIME:
                            this.strictUpdateFill(metaObject, UPDATE_TIME, () -> now, Date.class);
                            break;
                        case UPDATE_BY:
                            if (LoginSysUserContext.getLoginUserId() != null) {
                                Long userId = LoginSysUserContext.getLoginUserId();
                                this.strictUpdateFill(metaObject, UPDATE_BY, () -> userId, Long.class);

                            }
                            break;
                        default:
                            break;
                    }
                }
            } else {
                log.warn("mybatis-plus 自动更新 获取属性失败");
            }
        } else {
            log.info("任务调度默认值处理");
            Date now = new Date();
            for (String name : names) {
                switch (name) {
                    case UPDATE_TIME:
                        this.strictUpdateFill(metaObject, UPDATE_TIME, () -> now, Date.class);
                        break;
                    default:
                        break;
                }
            }
        }
    }

    @Override
    public MetaObjectHandler strictFillStrategy(MetaObject metaObject, String fieldName, Supplier<?> fieldVal) {
        Object obj = fieldVal.get();
        if (Objects.nonNull(obj)) {
            metaObject.setValue(fieldName, obj);
        }
        return this;
    }


}
